﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardGamesLibrary.Cards;
using CardGamesLibrary.Tables;
using CardGamesLibrary.Turns;

namespace CardGamesLibrary.Games
{
    public abstract class GameBase<TCard> : IGameBase<TCard> where TCard : Card
    {
        private int _turnCount = 0;

        protected GameBase(GameTable<TCard> table)
        {
           
            Table = table;
            
        }

        public void StartGame()
        {
            // init the table and rules
            Console.WriteLine("Game Started.");
            NextTurn();
        }

        
        public void NextTurn()
        {
            
            // play one gameturn
            Console.WriteLine("Playing turn {0:D2}.",_turnCount);
            CreateAndPlayNewTurn();
            _turnCount++;
            // evaluate if the game must be finished...
            if (_turnCount == 10)
            {
                Finished = true;
                Console.WriteLine("Game Finished @ turn {0:D2}.", _turnCount);
            }
        }

       
        public virtual void CreateAndPlayNewTurn()
        {
            GameTurn = new GameTurn(Table.Seats);
            while (!GameTurn.Finished)
            {
                GameTurn.Play();
            }
            
        }
 
        
        public Turn GameTurn
        { get; protected set; }

        public GameTable<TCard> Table 
        { get; private set; }

        public bool Finished 
        { get; private set; }

    }
}
